package com.bocai.yuweiwangluo.activity;


import android.app.AlertDialog;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.presenter.GameDetailPresenter;
import com.bocai.yuweiwangluo.utils.ImagePathInitUtil;
import com.bocai.yuweiwangluo.utils.SharePrefencesUtil;
import com.bocai.yuweiwangluo.utils.ShareUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.bocai.yuweiwangluo.viewModel.IGameDetailView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 比赛详情界面
 */
public class GameDetailActivity extends BaseActivity implements View.OnClickListener,IGameDetailView
{

    @ViewInject(R.id.web)
    WebView web;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private String game_id;
    private ShareUtil shareUtil;
    private GameDetailPresenter gameDetailPresenter;
    private String title="";
    private String url="";
    private String shareUrl="";
    private String shareImagePath="";
    private AlertDialog dialog;
    protected int contentView()
    {
        return R.layout.activity_game_detail;
    }
    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("比赛详情");
        mToolbar.inflateMenu(R.menu.menu_game_detail);
        game_id=getIntent().getStringExtra("game_id");
        title=getIntent().getStringExtra("title");
        url= UrlData.IMAGEURL+"/page/game?id=" + game_id + "&usr_id=" + SharePrefencesUtil.getUser_id(this);
        shareUrl=UrlData.IMAGEURL+"/page/game_share?id="+game_id+"&s=android";
        initEvent();
        getGameDetailPresenter();
        getData();

        new Thread(new Runnable() {
            @Override
            public void run() {
                shareImagePath=ImagePathInitUtil.initImagePath(GameDetailActivity.this);
            }
        }).start();
    }

    private void getData() {
        web.loadUrl(url);
        web.getSettings().setJavaScriptEnabled(true);
        web.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.endsWith("add?game_id=" + game_id)) {
                    gameDetailPresenter.joinGame();
                } else {
                    web.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                llRefresh.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        llRefresh.setOnClickListener(this);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                View view = LayoutInflater.from(GameDetailActivity.this).inflate(R.layout.dialog_share, null);
                dialog = new AlertDialog.Builder(GameDetailActivity.this)
                        .setView(view)
                        .show();
                setDialogView(view);
                return true;
            }
        });
    }
    private void setDialogView(View view)
    {
        shareUtil=new ShareUtil();
        LinearLayout ll_qq=(LinearLayout)view.findViewById(R.id.ll_dialog_qq);
        LinearLayout ll_weixin=(LinearLayout)view.findViewById(R.id.ll_dialog_weixin);
        LinearLayout ll_weixinfriend=(LinearLayout)view.findViewById(R.id.ll_dialog_weixinfriend);
        LinearLayout ll_sinaweibo=(LinearLayout)view.findViewById(R.id.ll_dialog_sinaweibo);
        ll_weixin.setOnClickListener(this);
        ll_weixinfriend.setOnClickListener(this);
        ll_qq.setOnClickListener(this);
        ll_sinaweibo.setOnClickListener(this);
    }
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                getData();
                break;
            case R.id.ll_dialog_qq:
                shareUtil.showShare(this,dialog,"qq",shareImagePath,
                        "赛事",title,shareUrl);
                break;
            case R.id.ll_dialog_weixin:
                shareUtil.showShare(this,dialog,"weixin",shareImagePath,
                        "赛事",title,shareUrl);
                break;
            case R.id.ll_dialog_weixinfriend:
                shareUtil.showShare(this,dialog,"weixinfriend",shareImagePath,
                        "赛事",title,shareUrl);
                break;
            case R.id.ll_dialog_sinaweibo:
                shareUtil.showShare(this,dialog,"sinaweibo",shareImagePath,
                        "赛事",title,shareUrl);
                break;

        }
    }

    @Override
    public String getGame_id() {
        return game_id;
    }

    @Override
    public void finishActivity() {
        this.finish();
    }

    @Override
    public GameDetailPresenter getGameDetailPresenter() {
        gameDetailPresenter=new GameDetailPresenter(this,this,this);
        return gameDetailPresenter;
    }
}
